package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;


import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //这个是解析我们的excle的表格,因为有很多条数据,所以返回的是一个list集合,而每一个list几个里面
            //装的是日期和预约人数.,所以这里用数组来封装的
            List<String[]> strings = POIUtils.readExcel(excelFile);
            //判断一下当前的解析
            if(strings!=null && strings.size()>0) {
                //这个是为了把我们的代码转化为我们容易解析的格式,所以new了一个 List<OrderSetting> list
                List<OrderSetting> list = new ArrayList<>();
                //遍历我们解析出来的excel
                for (String[] string : strings) {
                    //这个是我们的日期
                    String orderData=string[0];
                    //这个是我们的预约数量
                    String number = string[1];
                    //把这个封装在我们的ordersetting里面,由于在poi里面都转化为string封起来的,所以
                    //这里我们用new Date
                    OrderSetting orderSetting=new OrderSetting(new Date(orderData),Integer.parseInt(number));
                    //把封装在ordersetting里面的放进上面我们的List<OrderSetting> list里面
                    list.add(orderSetting);
                }
                //这样调用的就是我们转化后的
                orderSettingService.add(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){//这个date是从前台传过来的,格式是yyyy-MM
        try {
            List<Map>list=orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
